//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.ky;

import com.aidong.dao.wills.GcRoomDao;
import com.aidong.dao.wills.KyBespeakDao;
import com.aidong.dao.wills.KyTimeTableDao;
import com.aidong.dao.wills.PtCoachCodeDao;
import com.aidong.dao.wills.PtItemCodeDao;
import com.aidong.model.wills.KyTimeTable;
import com.aidong.web.controller.BaseController;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"kyTimeTable"})
public class KyTimeTableController extends BaseController {
    @Resource
    KyTimeTableDao kyTimeTableDao;
    @Resource
    KyBespeakDao kyBespeakDao;
    @Resource
    GcRoomDao gcRoomDao;
    @Resource
    PtItemCodeDao ptItemCodeDao;
    @Resource
    PtCoachCodeDao ptCoachCodeDao;

    public KyTimeTableController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        ArrayList ls = new ArrayList();
        Calendar cal = Calendar.getInstance();
        cal.set(2015, 6, 1);
        Date dt2 = cal.getTime();
        cal.setTime(new Date());
        cal.add(2, 1);

        while(cal.getTime().after(dt2)) {
            ls.add(sdf.format(cal.getTime()));
            cal.add(2, -1);
        }

        return (new ModelAndView("ky/timetable/list")).addObject("monthList", ls).addObject("month", sdf.format(new Date()));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "month",required = false) String month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date dt1 = this.parseDate(month + "01", sdf);
        if(dt1 == null) {
            dt1 = this.parseDate(sdf.format(new Date()).substring(0, 6) + "01", sdf);
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(dt1);
        cal.add(2, 1);
        Date dt2 = cal.getTime();
        SimpleDateFormat sdf1 = new SimpleDateFormat("M/d");
        ArrayList lsd = new ArrayList();
        cal.setTime(dt1);

        while(cal.getTime().before(dt2)) {
            lsd.add(sdf1.format(cal.getTime()));
            cal.add(5, 1);
        }

        LinkedHashMap hhmap = new LinkedHashMap();

        for(int list = 8; list < 23; ++list) {
            hhmap.put(String.valueOf(list), new ArrayList());
        }

        List var14 = this.kyTimeTableDao.getListByShopDay(this.getLoginShopCode(), dt1, dt2);
        Iterator mv = var14.iterator();

        while(mv.hasNext()) {
            KyTimeTable t = (KyTimeTable)mv.next();
            cal.setTime(t.getClassTime());
            int h = cal.get(11);
            if(h < 8) {
                h = 8;
            }

            if(h > 22) {
                h = 22;
            }

            List ls = (List)hhmap.get(String.valueOf(h));
            if(ls != null) {
                ls.add(t);
            }
        }

        ModelAndView var15 = new ModelAndView("ky/timetable/_list");
        var15.addObject("hhmap", hhmap);
        var15.addObject("lsd", lsd);
        var15.addObject("dt", dt1);
        return var15;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        KyTimeTable table = new KyTimeTable();
        table.setShopCode(this.getLoginShopCode());
        table.setPersonMax(Integer.valueOf(12));
        table.setStatus(Integer.valueOf(1));
        List itemList = this.ptItemCodeDao.getShopItemList(this.getLoginShopCode(), "07");
        List coachList = this.ptCoachCodeDao.getCoachListByType("04");
        List roomList = this.gcRoomDao.getListByShopCode(this.getLoginShopCode());
        ModelAndView mv = new ModelAndView("ky/timetable/edit");
        mv.addObject("table", table);
        mv.addObject("itemList", itemList);
        mv.addObject("coachList", coachList);
        mv.addObject("roomList", roomList);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("table") KyTimeTable table, @RequestParam("date") String date, @RequestParam("hh") String hh, @RequestParam("mm") String mm) {
        if(this.chkNull(table.getPtCode())) {
            return this.getResult(false, "请选择课程");
        } else if(this.chkNull(table.getCoachCode())) {
            return this.getResult(false, "请选择教练");
        } else if(!this.chkNull(date) && !this.chkNull(hh) && !this.chkNull(mm)) {
            if(hh.length() == 1) {
                hh = "0" + hh;
            }

            if(mm.length() == 1) {
                mm = "0" + mm;
            }

            table.setClassTime(this.parseDate(date + " " + hh + ":" + mm + ":00", "yyyy-MM-dd HH:mm:ss"));
            if(this.chkNull(table.getStatus())) {
                table.setStatus(Integer.valueOf(1));
            }

            table.setShopCode(this.getLoginShopCode());
            table.setBespeakQuantity(Integer.valueOf(0));
            table.setClearQuantity(Integer.valueOf(0));
            table.setCreateUser(this.getLoginUserCode());
            table.setCreateTime(new Date());
            table.setUpdateTime(new Date());

            try {
                this.kyTimeTableDao.add(table);
                this.addUserLog("新增K-Yoga课程表", "编号:" + table.getClassId() + ",课程:" + this.cache.getValue("ptItemName", table.getPtCode()));
            } catch (Exception var6) {
                this.log.error("add kytimetable", var6);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "保存完成");
        } else {
            return this.getResult(false, "请输入上课时间");
        }
    }

    @RequestMapping({"addm"})
    public ModelAndView addm() {
        KyTimeTable table = new KyTimeTable();
        table.setShopCode(this.getLoginShopCode());
        table.setPersonMax(Integer.valueOf(12));
        table.setStatus(Integer.valueOf(1));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        ArrayList monthList = new ArrayList();
        Calendar cal = Calendar.getInstance();
        monthList.add(sdf.format(cal.getTime()));
        cal.add(2, 1);
        monthList.add(sdf.format(cal.getTime()));
        List itemList = this.ptItemCodeDao.getShopItemList(this.getLoginShopCode(), "07");
        List coachList = this.ptCoachCodeDao.getCoachListByType("04");
        List roomList = this.gcRoomDao.getListByShopCode(this.getLoginShopCode());
        ModelAndView mv = new ModelAndView("ky/timetable/add");
        mv.addObject("table", table);
        mv.addObject("itemList", itemList);
        mv.addObject("coachList", coachList);
        mv.addObject("roomList", roomList);
        mv.addObject("monthList", monthList);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_addm"})
    @ResponseBody
    public String _addm(@ModelAttribute("table") KyTimeTable table, @RequestParam("month") String month, @RequestParam("times") String times) {
        if(this.chkNull(table.getPtCode())) {
            return this.getResult(false, "请选择课程");
        } else if(this.chkNull(table.getCoachCode())) {
            return this.getResult(false, "请选择教练");
        } else if(this.chkNull(month)) {
            return this.getResult(false, "请选择月份");
        } else if(this.chkNull(times)) {
            return this.getResult(false, "请输入时间");
        } else {
            HashMap wt = new HashMap();
            String[] ss = times.split("\\|");
            String[] sdf = ss;
            int sdf1 = ss.length;

            for(int list = 0; list < sdf1; ++list) {
                String cal = sdf[list];
                String[] dt2 = cal.split(",");
                Integer e = this.parseInt(dt2[0]);
                wt.put(e, dt2[1]);
            }

            SimpleDateFormat var15 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat var16 = new SimpleDateFormat("yyyy-MM-dd");
            ArrayList var17 = new ArrayList();
            Calendar var18 = Calendar.getInstance();
            var18.setTime(this.parseDate(month + "01", "yyyyMMdd"));
            var18.add(2, 1);
            Date var19 = var18.getTime();
            var18.add(2, -1);

            for(; var18.getTime().before(var19); var18.add(5, 1)) {
                int var20 = var18.get(7);
                String hh = (String)wt.get(Integer.valueOf(var20));
                if(hh != null && var18.getTime().after(new Date())) {
                    KyTimeTable table1 = new KyTimeTable();
                    table1.setShopCode(this.getLoginShopCode());
                    table1.setClassTime(this.parseDate(var16.format(var18.getTime()) + " " + hh + ":00", var15));
                    table1.setPtCode(table.getPtCode());
                    table1.setCoachCode(table.getCoachCode());
                    table1.setRoomCode(table.getRoomCode());
                    table1.setPersonMax(table.getPersonMax());
                    table1.setBespeakQuantity(Integer.valueOf(0));
                    table1.setClearQuantity(Integer.valueOf(0));
                    table1.setStatus(Integer.valueOf(1));
                    table1.setCreateUser(this.getLoginUserCode());
                    table1.setCreateTime(new Date());
                    table1.setUpdateTime(new Date());
                    var17.add(table1);
                }
            }

            try {
                this.kyTimeTableDao.addTimeTable(var17);
                this.addUserLog("新增K-Yoga课程表", "课程:" + this.cache.getValue("ptItemName", table.getPtCode()));
            } catch (Exception var14) {
                this.log.error("add kytimetable", var14);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "保存完成");
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("id") int id) {
        KyTimeTable table = (KyTimeTable)this.kyTimeTableDao.getById(Integer.valueOf(id));
        if(table == null) {
            return this.showAlert("没有这个课程");
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(table.getClassTime());
            int hh = cal.get(11);
            int mm = cal.get(12);
            List itemList = this.ptItemCodeDao.getShopItemList(this.getLoginShopCode(), "07");
            List coachList = this.ptCoachCodeDao.getCoachListByType("04");
            List roomList = this.gcRoomDao.getListByShopCode(this.getLoginShopCode());
            ModelAndView mv = new ModelAndView("ky/timetable/edit");
            mv.addObject("table", table);
            mv.addObject("itemList", itemList);
            mv.addObject("coachList", coachList);
            mv.addObject("roomList", roomList);
            mv.addObject("hh", Integer.valueOf(hh));
            mv.addObject("mm", Integer.valueOf(mm));
            mv.addObject("ed", Integer.valueOf(table.getClassTime().getTime() > System.currentTimeMillis()?1:0));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("table") KyTimeTable table, @RequestParam("date") String date, @RequestParam("hh") String hh, @RequestParam("mm") String mm) {
        if(this.chkNull(table.getClassId())) {
            return this.getResult(false, "没有这个课程");
        } else if(this.chkNull(table.getPtCode())) {
            return this.getResult(false, "请选择课程");
        } else if(this.chkNull(table.getCoachCode())) {
            return this.getResult(false, "请选择教练");
        } else if(!this.chkNull(date) && !this.chkNull(hh) && !this.chkNull(mm)) {
            if(hh.length() == 1) {
                hh = "0" + hh;
            }

            if(mm.length() == 1) {
                mm = "0" + mm;
            }

            table.setClassTime(this.parseDate(date + " " + hh + ":" + mm + ":00", "yyyy-MM-dd HH:mm:ss"));
            if(this.chkNull(table.getStatus())) {
                table.setStatus(Integer.valueOf(1));
            }

            KyTimeTable table1 = (KyTimeTable)this.kyTimeTableDao.getById(table.getClassId());
            if(table1 == null) {
                return this.getResult(false, "没有这个课程");
            } else if(table.getClassTime().getTime() < System.currentTimeMillis()) {
                return this.getResult(false, "已经上课,不能修改课程");
            } else {
                table1.setPtCode(table.getPtCode());
                table1.setCoachCode(table.getCoachCode());
                table1.setRoomCode(table.getRoomCode());
                table1.setClassTime(table.getClassTime());
                table1.setPersonMax(table.getPersonMax());
                table1.setNote(table.getNote());
                table1.setUpdateTime(new Date());

                try {
                    this.kyTimeTableDao.updateTimeTable(table1);
                    this.addUserLog("修改K-Yoga课程表", "编号:" + table.getClassId() + ",课程:" + this.cache.getValue("ptItemName", table.getPtCode()));
                } catch (Exception var7) {
                    this.log.error("edit kytimetable", var7);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        } else {
            return this.getResult(false, "请输入上课时间");
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("id") int id) {
        KyTimeTable table = (KyTimeTable)this.kyTimeTableDao.getById(Integer.valueOf(id));
        if(table == null) {
            return this.getResult(false, "没有这个教室");
        } else {
            try {
                this.kyTimeTableDao.updateStatus(id, 0);
                this.addUserLog("删除K-Yoga课程表", "编号:" + table.getClassId() + ",课程:" + this.cache.getValue("ptItemName", table.getPtCode()));
            } catch (Exception var4) {
                this.log.error("del kytimetable", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}
